#!/usr/bin/env python
# -*- coding: cp1252 -*-

class Budget:
    Stores = dict()
    __ItemsCodeHash__ = dict()

    def __init__(self):
        self.Stores = dict()
        self.__ItemsCodeHash__ = dict()

    def add(self, store, storerating, code, desc, color, colornum, qtyneed, qtyavai, price):
        if store in self.Stores:
            budget  = self.Stores[store]
        else:
            budget = __BudgetStore__(store, storerating)

        budget.add(code, desc, color, colornum, qtyneed, qtyavai, price)
        self.Stores[store] = budget

        if code not in self.__ItemsCodeHash__:
            self.__ItemsCodeHash__[(code, colornum)] = 1

    def saveBudgetReport(self, filename):
        stores = self.Stores.values()
        stores.sort(key=lambda store: len(store.Items), reverse=True)

        with open(filename, 'w') as f:
            for store in stores:
                f.write("{:<40}{:>8d}{:>8d}{:>10.2f}\n".format(store.Store.encode('ascii', 'replace') + " " + store.StoreRating, len(store.Items), store.TotalBricks, store.Value))
        f.closed

class __BudgetStore__:
    Store = ""
    StoreRating = ""
    Items = dict()
    Value = 0.0
    TotalBricks = 0

    def __init__(self, store, storerating):
        self.Store = store
        self.StoreRating = storerating
        self.Items = dict()
        self.Value = 0.0
        self.TotalBricks = 0

    def add(self, code, desc, color, colornum, qtyneed, qtyavai, price):

        item = __BudgetItem__()

        item.Code = code
        item.Description = desc
        item.Color = color
        item.ColorNumber = colornum
        item.QuantityNeeded = qtyneed
        item.QuantityAvailable = qtyavai
        item.Price = price

        self.Items[code] = item

        self.Value += min(qtyneed, qtyavai) * price
        self.TotalBricks += min(qtyneed, qtyavai)

class __BudgetItem__:
    Code = ""
    Description = ""
    Color = ""
    ColorNumber = 0
    QuantityNeeded = 0
    QuantityAvailable = 0
    Price = 0.0
